package com.eco.fanliapp.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by mars on 8/5/16.
 */
public class MapValueHelper {
	
    private Map map;

    public MapValueHelper(Map<String, Object> map) {
        this.map = map;
    }

    public String getString(String key) {
        try {
            Object value = map.get(key);
            if (value instanceof String) return ((String) map.get(key)).trim();
            if (value instanceof Number) return value.toString();
            return null;
        } catch (Exception e) {

        }
        return null;
    }

    public float getFloat(String key) {
        Object value = map.get(key);
        if (value instanceof Number) return ((Number) value).floatValue();
        if (value instanceof String) {
            try {
                return Integer.valueOf(((String) value).trim());
            } catch (Exception e) {
                return 0;
            }
        }
        return 0;
    }

    public float getFloat(String key, int def) {
        Object value = map.get(key);
        if (value instanceof Number) return ((Number) value).floatValue();
        if (value instanceof String) {
            try {
                return Integer.valueOf(((String) value).trim());
            } catch (Exception e) {
                return def;
            }
        }
        return def;
    }

    public int getInt(String key) {
        Object value =  map.get(key);
        if (value instanceof Number) return ((Number) value).intValue();
        if (value instanceof String) {
            try {
                return Integer.valueOf(((String) value).trim());
            } catch (Exception e) {
                return 0;
            }
        }
        return 0;
    }

    public int getInt(String key, int def) {
        Object value =  map.get(key);
        if (value instanceof Number) return ((Number) value).intValue();
        if (value instanceof String) {
            try {
                return Integer.valueOf((String) value);
            } catch (Exception e) {
                return def;
            }
        }
        return def;
    }

    public Double getDouble(String key) {
        Object value =  map.get(key);
        if (value instanceof String) {
            try {
                return new Double(((String) value).trim());
            } catch (Exception e) {
                return new Double(0);
            }
        }
        if (value instanceof Number) return new Double(value.toString());
        return new Double(0);
    }

    public BigDecimal getBigDecimal(String key) {
        Object value =  map.get(key);
        if (value instanceof String) {
            try {
                return new BigDecimal(((String) value).trim());
            } catch (Exception e) {
                return new BigDecimal(0);
            }
        }
        if (value instanceof Number) return new BigDecimal(value.toString());
        return new BigDecimal(0);
    }

    public boolean getBool(String key) {
        Object value =  map.get(key);
        if (value instanceof Boolean) return (boolean) value;
        return false;
    }

    public Map<String, Object> getMap(String key) {
        Object value =  map.get(key);
        if (value instanceof Map) return (Map<String, Object>) value;
        return new HashMap<>();
    }

    public Map<String, String> getStringMap(String key) {
        Object value =  map.get(key);
        if (value instanceof Map) return (Map<String, String>) value;
        return new HashMap<>();
    }

    public List<Map<String, Object>> getList(String key) {
        Object value =  map.get(key);
        if (value instanceof List) return (List<Map<String, Object>>) value;
        if (value instanceof String) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                return mapper.readValue((String) value, List.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<>();
    }

    public List<String> getStringList(String key) {
        Object value =  map.get(key);
        if (value instanceof List) return (List<String>) value;
        if (value instanceof String) {
            String str = (String) value;
            if (str.length() == 0) {
                return new ArrayList<>();
            }
            String[] l = str.split(",");
            return Arrays.asList(l);
        }
        return new ArrayList<>();
    }

    public long getLong(String key) {
        Object value =  map.get(key);
        if (value instanceof Number) return ((Number) value).longValue();
        if (value instanceof String) {
            try {
                return Long.valueOf(((String) value).trim());
            } catch (Exception e) {
                return 0;
            }
        }
        return 0;
    }

    public Timestamp getTimestamp(String key) {
        long l = getLong(key);
        if (String.valueOf(l).length() == 10) {
            l *= 1000;
        }
        return new Timestamp(l);
    }

    public String getDate(String key) {
        long timestamp = getLong(key);
        if (timestamp == 0) return null;
        Date date = new Date(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}
